﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Windows.Forms;


public class OleDBHelper
{
    /// <summary>
    /// Get Sql Connection in Web.config
    /// </summary>
    public static OleDbConnection GetConnection
    {
        get
        {
            return So_Tu_Phap.ConnectToDB.getConnection();
        }
    }
    //public static string GetConnection
    //{
    //    get
    //    {
    //        return @"Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=SoTuPhapDB.accdb";
    //        //return demo2.ConnectToDB.connectionString.ToString(); 
    //        //System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    //    }
    //}

    /// <summary>
    /// Execute an update store
    /// </summary>
    /// <param name="storeName">Store procedure name</param>
    /// <param name="pars">Sql Parameter command</param>
    /// <returns></returns>
    public static int ExecuteNonQuery(string storeName, OleDbParameter[] pars, Boolean check)
    {
        if (string.IsNullOrEmpty(storeName))
            return 0;
        else
        {
            OleDbCommand cmd = new OleDbCommand(storeName, GetConnection);

            if (check)
                cmd.CommandType = CommandType.StoredProcedure;
            else
                cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddRange(pars);

            return cmd.ExecuteNonQuery();
        }
    }

    /// <summary>
    /// Execute an command text
    /// </summary>
    /// <param name="commandText"></param>
    /// <returns></returns>
    public static int ExecuteNonQuery(string commandText)
    {
        if (string.IsNullOrEmpty(commandText))
            return 0;
        else
        {
            OleDbCommand cmd = new OleDbCommand(commandText, GetConnection);
            cmd.CommandType = CommandType.Text;
            return cmd.ExecuteNonQuery();
        }
    }

    /// <summary>
    /// Get DataTable by Store Procedure
    /// </summary>
    /// <param name="storeName"></param>
    /// <param name="pars"></param>
    /// <returns></returns>
    public static DataTable ExecuteDataTable(string storeName, OleDbParameter[] pars)
    {
        DataTable results = new DataTable();
        OleDbCommand cmd = new OleDbCommand(storeName, GetConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddRange(pars);
        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
        adapter.Fill(results);
        return results;
    }

    /// <summary>
    /// Get DataTable by command text
    /// </summary>
    /// <param name="commandLine"></param>
    /// <returns></returns>
    public static DataTable ExecuteDataTable(string commandText)
    {
        DataTable results = new DataTable();
        OleDbCommand cmd = new OleDbCommand(commandText, GetConnection);
        cmd.CommandType = CommandType.Text;
        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
        adapter.Fill(results);
        return results;
    }

    /// <summary>
    /// Get as OleDbDataReader
    /// </summary>
    /// <param name="commandText">command text</param>
    /// <returns></returns>
    public static OleDbDataReader ExecuteDataReader(string commandText)
    {
        OleDbCommand command = new OleDbCommand(commandText, GetConnection);
        return command.ExecuteReader();
    }

    /// <summary>
    /// Get as OleDbDataReader
    /// </summary>
    /// <param name="storeName">store name</param>
    /// <param name="pars"></param>
    /// <returns></returns>
    public static OleDbDataReader ExecuteDataReader(string storeName, OleDbParameter[] pars)
    {
        OleDbCommand command = new OleDbCommand(storeName, GetConnection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddRange(pars);
        return command.ExecuteReader();
    }

    /// <summary>
    /// Get as Object
    /// </summary>
    /// <param name="storeName"></param>
    /// <param name="pars"></param>
    /// <returns></returns>
    public static object ExecuteScalar(string storeName, OleDbParameter[] pars, Boolean check)
    {
        OleDbCommand cmd = new OleDbCommand(storeName, GetConnection);
        if (check)
            cmd.CommandType = CommandType.StoredProcedure;
        else
            cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddRange(pars);
        return cmd.ExecuteScalar();
    }

    /// <summary>
    /// Get as object
    /// </summary>
    /// <param name="commandText"></param>
    /// <returns></returns>
    public static object ExecuteScalar(string commandText)
    {
        OleDbCommand cmd = new OleDbCommand(commandText, GetConnection);
        cmd.CommandType = CommandType.Text;
        return cmd.ExecuteScalar();
    }
}

